package com.ruoyi.system.service.module.event.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.TransactionalException;
import com.ruoyi.system.domain.module.event.*;
import com.ruoyi.system.mapper.module.event.*;
import com.ruoyi.system.mapper.system.SysUserMapper;
import com.ruoyi.system.service.module.event.IEventPlanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * 巡检巡更、保洁等 执行计划Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-25
 */
@Service
public class EventPlanServiceImpl extends ServiceImpl<EventPlanMapper, EventPlan> implements IEventPlanService {

	@Resource
	private EventPlanMapper eventPlanMapper;
	@Resource
	private EventPlanWorkMapper eventPlanWorkMapper;
	@Resource
	private EventPlanTaskMapper eventPlanTaskMapper;
	@Resource
	private EventPlanTaskInfoMapper eventPlanTaskInfoMapper;
	@Resource
	private EventTerminalsMapper eventTerminalsMapper;
	@Resource
	private EventPlanTaskLeaderMapper planTaskLeaderMapper;
	@Resource
	private EventPatrolLineInfoMapper patrolLineInfoMapper;
	@Resource
	private EventCleanAreaListInfoMapper cleanAreaListInfoMapper;
	@Resource
	private SysUserMapper sysUserMapper;
	@Resource
	private EventPatrolPointMapper eventPatrolPointMapper;

	@Override
	public int addUserWork(EventPlan eventPlan) {
		int ret = 0;
		try {
			//获取区间内的天数
			ArrayList<Date> day = new ArrayList<>();
			Instant begin = eventPlan.getBeginTime().toInstant();
			Instant end = eventPlan.getEndTime().toInstant();
			ZoneId zoneId = ZoneId.systemDefault();
			LocalDate beginTime = begin.atZone(zoneId).toLocalDate();
			LocalDate endTime = end.atZone(zoneId).toLocalDate();
			//两个日期中间有几天
//			long days = ChronoUnit.DAYS.between(beginTime, endTime);
			//计划周期
			//当计划频率为日
			if (eventPlan.getCycleType().equals("day")) {
				day = selectDayType(beginTime, endTime);
			}
			//当计划周期不为日
			if (eventPlan.getCycleDays() != null) {
				eventPlan.setCycleDay(Arrays.asList(eventPlan.getCycleDays().split(",")));
			}
			if (eventPlan.getCycleDay() != null) {
				String str = "";
				for (int i = 0; i < eventPlan.getCycleDay().size(); i++) {
					str += eventPlan.getCycleDay().get(i);
					if (i != eventPlan.getCycleDay().size() - 1) {
						str += ",";
					}

					//当计划频率为周
					if (eventPlan.getCycleType().equals("week")) {
						ArrayList<Date> weekDays = selectWeekType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : weekDays) {
							day.add(date);
						}
					}
					//当计划频率为月
					else if (eventPlan.getCycleType().equals("month")) {
						ArrayList<Date> monthDays = selectMonthType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
					//当计划频率为季
					else if (eventPlan.getCycleType().equals("season")) {
						ArrayList<Date> monthDays = selectSeasonType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
					//当计划频率为半年
					else if (eventPlan.getCycleType().equals("half-year")) {
						ArrayList<Date> monthDays = selectHalfYearType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
					//当计划频率为年
					else if (eventPlan.getCycleType().equals("year")) {
						ArrayList<Date> monthDays = selectYearType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
				}
				eventPlan.setCycleDays(str);
			}
			SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(eventPlan.getUserId()));
			//生成计划
			//查询work并赋值
			for (Date date : day) {
				EventPlanWork work = new EventPlanWork();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String time = simpleDateFormat.format(date);
				String beginTime1 = time.replace("00:00:00", eventPlan.getBeginRangeTime() + ":00");
				String endTime1 = time.replace("00:00:00", eventPlan.getEndRangeTime() + ":00");
//				Date beginDate1 = simpleDateFormat.parse(beginTime1);
//				Date endDate1 = simpleDateFormat.parse(endTime1);
				QueryWrapper<EventPlanWork> qw = new QueryWrapper<>();
				qw.like("begin_time", time);
				qw.eq("event_plan_id", eventPlan.getId());
				qw.eq("estate_id", eventPlan.getEstateId());
				qw.eq("event_target_id", eventPlan.getGroupId());
				qw.like("begin_time", eventPlan.getBeginRangeTime());
				qw.like("end_time", eventPlan.getEndRangeTime());
				List<EventPlanWork> workList = eventPlanWorkMapper.selectList(qw);
				if (workList.size() > 0) {
					work = workList.get(0);
				}
				//生成 info
				//巡检
//				if (eventPlan.getEventTypeId() == 1 || eventPlan.getEventTypeId() == 6) {
//					//完成方式 0 - 任意一人完成  1 - 每人都完成  2 - 指定人完成
//					QueryWrapper<EventTerminals> terminals = new QueryWrapper<>();
//					terminals.eq("group_id", eventPlan.getGroupId());
//					List<EventTerminals> terminals1 = eventTerminalsMapper.selectList(terminals);
//					if (CollUtil.isNotEmpty(terminals1)) {
//						for (EventTerminals terminals2 : terminals1) {
//							EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
////									taskInfo.setId(null);
//							taskInfo.setEventWorkId(work.getId());
//							taskInfo.setState(0L);
//							taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
//							taskInfo.setTargetId(terminals2.getId());
//							if (eventPlan.getEventTypeId() == 1) {
//								taskInfo.setTargetType(0);
//							} else {
//								taskInfo.setTargetType(5);
//							}
//							ret = eventPlanTaskInfoMapper.insert(taskInfo);
//							if (ret <= 0) {
//								throw new TransactionalException("eventPlanTaskInfoMapper insert error");
//							}
//						}
//					}
//				}
				//巡更
//				if (eventPlan.getEventTypeId() == 2) {
//					QueryWrapper<EventPatrolLineInfo> lineInfo = new QueryWrapper<>();
//					lineInfo.eq("line_id", eventPlan.getGroupId());
//					List<EventPatrolLineInfo> lineInfos = patrolLineInfoMapper.selectList(lineInfo);
//					if (CollUtil.isNotEmpty(lineInfos)) {
//						for (EventPatrolLineInfo lineInfo1 : lineInfos) {
//							EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
//							taskInfo.setEventWorkId(work.getId());
//							taskInfo.setState(0L);
//							taskInfo.setTargetType(1);
//							taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
//							EventPatrolLineInfo line = patrolLineInfoMapper.selectById(lineInfo1.getId());
//							if (line != null) {
//								taskInfo.setTargetId(line.getPointId());
//							}
//							ret = eventPlanTaskInfoMapper.insert(taskInfo);
//
//							if (ret <= 0) {
//								throw new TransactionalException("eventPlanTaskInfoMapper insert error");
//							}
//						}
//					}
//
//				}
				//保洁、绿化、消杀
//				else if (eventPlan.getEventTypeId() == 3 || eventPlan.getEventTypeId() == 4 || eventPlan.getEventTypeId() == 5) {
//					QueryWrapper<EventCleanAreaInfo> areaInfo = new QueryWrapper<>();
//					areaInfo.eq("area_id", eventPlan.getGroupId());
//					List<EventCleanAreaInfo> areaInfos = eventCleanAreaInfoMapper.selectList(areaInfo);
//					if (CollUtil.isNotEmpty(areaInfos)) {
//						for (EventCleanAreaInfo areaInfo1 : areaInfos) {
//							EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
//							taskInfo.setEventWorkId(work.getId());
//							taskInfo.setState(0L);
//							taskInfo.setTargetType(2);
//							taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
//							taskInfo.setTargetId(areaInfo1.getId());
//							ret = eventPlanTaskInfoMapper.insert(taskInfo);
//
//							if (ret <= 0) {
//								throw new TransactionalException("eventPlanTaskInfoMapper insert error");
//							}
//						}
//					}
//				}
//				else if (eventPlan.getEventTypeId() == 3|| eventPlan.getEventTypeId() == 4 || eventPlan.getEventTypeId() == 5) {
//					QueryWrapper<EventCleanAreaListInfo> listInfo = new QueryWrapper<>();
//					listInfo.eq("area_list_id", eventPlan.getGroupId());
//					List<EventCleanAreaListInfo> lineInfos = cleanAreaListInfoMapper.selectList(listInfo);
//					if (CollUtil.isNotEmpty(lineInfos)) {
//						for (EventCleanAreaListInfo lineInfo1 : lineInfos) {
//							EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
//							taskInfo.setEventWorkId(work.getId());
//							taskInfo.setState(0L);
//							taskInfo.setTargetType(2);
//							taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
//							EventCleanAreaListInfo line = cleanAreaListInfoMapper.selectById(lineInfo1.getId());
//							if (line != null) {
//								taskInfo.setTargetId(Math.toIntExact(line.getAreaId()));
//							}
//							ret = eventPlanTaskInfoMapper.insert(taskInfo);
//
//							if (ret <= 0) {
//								throw new TransactionalException("eventPlanTaskInfoMapper insert error");
//							}
//						}
//					}
//				}

				//生成 task
				EventPlanTask task = new EventPlanTask();
				task.setPropertyId(eventPlan.getPropertyId());
				task.setEstateId(eventPlan.getEstateId());
				task.setEventPlanId(eventPlan.getId());
				task.setEventType(eventPlan.getEventTypeId());
				task.setEventTypeName(eventPlan.getEventTypeName());
				task.setEventTargetId(Integer.valueOf(eventPlan.getGroupId()));
				task.setWorkId(work.getId());
				if (eventPlan.getUserId() != null) {
					if (eventPlan.getUserId() != null) {
						task.setWorkerSysUserId(Math.toIntExact(sysUser.getUserId()));
					}
					if (sysUser.getNickName() != null) {
						task.setWorkerName(sysUser.getNickName());
					}
					if (sysUser.getWxUserId() != null) {
						task.setWorkerWxUserId(sysUser.getWxUserId());
					}
					if (sysUser.getPhoneNum() != null) {
						task.setWorkerPhone(sysUser.getPhoneNum());
					}
				}
				task.setState(0);
				task.setTaskBeginTime(eventPlan.getBeginRangeTime());
				task.setTaskEndTime(eventPlan.getEndRangeTime());
				SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String time1 = simpleDateFormat1.format(date);
				String beginTime2 = time1.replace("00:00:00", eventPlan.getBeginRangeTime() + ":00");
				String endTime2 = time1.replace("00:00:00", eventPlan.getEndRangeTime() + ":00");
				try {
					Date beginDate = simpleDateFormat1.parse(beginTime2);
					Date endDate = simpleDateFormat1.parse(endTime2);
					task.setBeginTime(beginDate);
					task.setEndTime(endDate);
				} catch (ParseException e) {
					throw new RuntimeException(e);
				}
				ret = eventPlanTaskMapper.insert(task);
				if (ret <= 0) {
					throw new TransactionalException("eventPlanTaskMapper insert error");
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
			throw new TransactionalException("addPlan System error");
		}
		return ret;
	}

	@Override
	@Transactional
	public int addPlan(EventPlan eventPlan) {
		int ret = 0;
		try {

			//获取区间内的天数
			ArrayList<Date> day = new ArrayList<>();
			Instant begin = eventPlan.getBeginTime().toInstant();
			Instant end = eventPlan.getEndTime().toInstant();
			ZoneId zoneId = ZoneId.systemDefault();
			LocalDate beginTime = begin.atZone(zoneId).toLocalDate();
			LocalDate endTime = end.atZone(zoneId).toLocalDate();
			//两个日期中间有几天
//			long days = ChronoUnit.DAYS.between(beginTime, endTime);
			//计划周期
			//当计划频率为日
			if (eventPlan.getCycleType().equals("day")) {
				day = selectDayType(beginTime, endTime);
			}
			//当计划周期不为日
			if (eventPlan.getCycleDay() != null) {
				String str = "";
				for (int i = 0; i < eventPlan.getCycleDay().size(); i++) {
					str += eventPlan.getCycleDay().get(i);
					if (i != eventPlan.getCycleDay().size() - 1) {
						str += ",";
					}

					//当计划频率为周
					if (eventPlan.getCycleType().equals("week")) {
						ArrayList<Date> weekDays = selectWeekType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : weekDays) {
							day.add(date);
						}
					}
					//当计划频率为月
					else if (eventPlan.getCycleType().equals("month")) {
						ArrayList<Date> monthDays = selectMonthType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
					//当计划频率为季
					else if (eventPlan.getCycleType().equals("season")) {
						ArrayList<Date> monthDays = selectSeasonType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
					//当计划频率为半年
					else if (eventPlan.getCycleType().equals("half-year")) {
						ArrayList<Date> monthDays = selectHalfYearType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
					//当计划频率为年
					else if (eventPlan.getCycleType().equals("year")) {
						ArrayList<Date> monthDays = selectYearType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
				}
				eventPlan.setCycleDays(str);
			}
			ret = eventPlanMapper.insert(eventPlan);
			if (ret <= 0) {
				throw new TransactionalException("eventPlanMapper insert error");
			}

			//添加 巡视人
			if (eventPlan.getLeaderId() != null) {
				EventPlanTaskLeader leader = new EventPlanTaskLeader();
				leader.setEventPlanId(eventPlan.getId());
				leader.setLeaderSysUserId(eventPlan.getLeaderId());
				leader.setState(0);
				SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(eventPlan.getLeaderId()));
				if (sysUser != null) {
					if (sysUser.getWxUserId() != null) {
						leader.setLeaderWxUserId(sysUser.getWxUserId());
					}
				}
				ret = planTaskLeaderMapper.insert(leader);
				if (ret <= 0) {
					throw new TransactionalException("planTaskLeaderMapper insert error");
				}
			}

			for (EventPlanInfo info : eventPlan.getEventPlanInfos()) {
				List<SysUser> sysUsers = new ArrayList<>();
				for (int id : info.getUserId()) {
					SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(id));
					sysUsers.add(sysUser);
				}
				//生成计划
				//生成work
				for (Date date : day) {
					EventPlanWork work = new EventPlanWork();
					work.setEventPlanId(eventPlan.getId());
					work.setState(0L);
					work.setPropertyId(eventPlan.getPropertyId());
					work.setFinishType(info.getFinishType());
					work.setEstateId(eventPlan.getEstateId());
					work.setEventTargetId(info.getGroupId());
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String time = simpleDateFormat.format(date);
					String beginTime1 = time.replace("00:00:00", info.getBeginRangeTime() + ":00");
					String endTime1 = time.replace("00:00:00", info.getEndRangeTime() + ":00");
					try {
						Date beginDate = simpleDateFormat.parse(beginTime1);
						Date endDate = simpleDateFormat.parse(endTime1);
						work.setBeginTime(beginDate);
						work.setEndTime(endDate);
					} catch (ParseException e) {
						throw new RuntimeException(e);
					}
					ret = eventPlanWorkMapper.insert(work);
					if (ret <= 0) {
						throw new TransactionalException("eventPlanWorkMapper insert error");
					}

					//生成 info
					//巡检
					if (eventPlan.getEventTypeId() == 1 || eventPlan.getEventTypeId() == 6) {
						//完成方式 0 - 任意一人完成  1 - 每人都完成  2 - 指定人完成
						if (info.getFinishType() == 0) {
							QueryWrapper<EventTerminals> terminals = new QueryWrapper<>();
							terminals.eq("group_id", info.getGroupId());
//							terminals.ne("state",0);
							List<EventTerminals> terminals1 = eventTerminalsMapper.selectList(terminals);
							//判断该点是否被禁用
							List<EventTerminals> terminals0 = new ArrayList<>();
							for(EventTerminals terminals3:terminals1){
								if (terminals3.getState()!=0){
									terminals0.add(terminals3);
								}
							}
							if (CollUtil.isNotEmpty(terminals0)) {
								for (EventTerminals terminals2 : terminals0) {
									EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
//									taskInfo.setId(null);
									taskInfo.setEventWorkId(work.getId());
									taskInfo.setState(0L);
									taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
									taskInfo.setTargetId(terminals2.getId());
									if (eventPlan.getEventTypeId() == 1) {
										taskInfo.setTargetType(0);
									} else {
										taskInfo.setTargetType(5);
									}

									ret = eventPlanTaskInfoMapper.insert(taskInfo);

									if (ret <= 0) {
										throw new TransactionalException("eventPlanTaskInfoMapper insert error");
									}
								}
							}
						} else {
							// 完成方式  1 - 每人都完成  2 - 指定人完成
							// 所有人都添加一条数据
							for (Integer user : info.getUserId()) {
								SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(user));
								QueryWrapper<EventTerminals> terminals = new QueryWrapper<>();
								terminals.eq("group_id", info.getGroupId());
//								terminals.ne("state",0);
								List<EventTerminals> terminals1 = eventTerminalsMapper.selectList(terminals);
								//判断该点是否被禁用
								List<EventTerminals> terminals0 = new ArrayList<>();
								for(EventTerminals terminals3:terminals1){
									if (terminals3.getState()!=0){
										terminals0.add(terminals3);
									}
								}
								if (CollUtil.isNotEmpty(terminals0)) {
									for (EventTerminals terminals2 : terminals0) {
										EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
										taskInfo.setEventWorkId(work.getId());
										taskInfo.setState(0L);
//										taskInfo.setId(null);
										taskInfo.setTargetType(0);
										if (sysUser != null) {
											taskInfo.setSysUserId(Long.valueOf(user));
											if (sysUser.getNickName() != null) {
												taskInfo.setSysUserName(sysUser.getNickName());
											}
											if (sysUser.getWxUserId() != null) {
												taskInfo.setWxUserId(Long.valueOf(sysUser.getWxUserId()));
											}
										}
										taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
										taskInfo.setTargetId(terminals2.getId());
										ret = eventPlanTaskInfoMapper.insert(taskInfo);
										if (ret <= 0) {
											throw new TransactionalException("eventPlanTaskInfoMapper insert error");
										}
									}
								}
							}
						}
					}
					//巡更
					else if (eventPlan.getEventTypeId() == 2) {
						if (info.getFinishType() == 0) {
							QueryWrapper<EventPatrolLineInfo> lineInfo = new QueryWrapper<>();
							lineInfo.eq("line_id", info.getGroupId());
//							lineInfo.ne("state",0);
							List<EventPatrolLineInfo> lineInfos = patrolLineInfoMapper.selectList(lineInfo);
							//判断该点是否被禁用
							List<EventPatrolPoint> lineInfos0 = new ArrayList<>();
							for(EventPatrolLineInfo lineInfos1:lineInfos){
								QueryWrapper<EventPatrolPoint> qwPoint = new QueryWrapper<>();
								qwPoint.eq("id", lineInfos1.getPointId());
								List<EventPatrolPoint> point = eventPatrolPointMapper.selectList(qwPoint);
								if (point.size()>0){
									if (point.get(0).getState()!=0){
										lineInfos0.add(point.get(0));
									}
								}

							}
							if (CollUtil.isNotEmpty(lineInfos)) {
								for (EventPatrolLineInfo lineInfo1 : lineInfos) {
									EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
									taskInfo.setEventWorkId(work.getId());
									taskInfo.setState(0L);
									taskInfo.setTargetType(1);
									taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
									EventPatrolLineInfo line = patrolLineInfoMapper.selectById(lineInfo1.getId());
									if (line != null) {
										taskInfo.setTargetId(line.getPointId());
									}
									ret = eventPlanTaskInfoMapper.insert(taskInfo);

									if (ret <= 0) {
										throw new TransactionalException("eventPlanTaskInfoMapper insert error");
									}
								}
							}
						} else {
							for (Integer user : info.getUserId()) {
								SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(user));
								QueryWrapper<EventPatrolLineInfo> lineInfo = new QueryWrapper<>();
								lineInfo.eq("line_id", info.getGroupId());
//								lineInfo.ne("state",0);
								List<EventPatrolLineInfo> lineInfos = patrolLineInfoMapper.selectList(lineInfo);
								if (CollUtil.isNotEmpty(lineInfos)) {
									for (EventPatrolLineInfo lineInfo1 : lineInfos) {
										EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
										taskInfo.setEventWorkId(work.getId());
										if (sysUser != null) {
											taskInfo.setSysUserId(Long.valueOf(user));
											if (sysUser.getNickName() != null) {
												taskInfo.setSysUserName(sysUser.getNickName());
											}
											if (sysUser.getWxUserId() != null) {
												taskInfo.setWxUserId(Long.valueOf(sysUser.getWxUserId()));
											}
										}
										taskInfo.setState(0L);
										taskInfo.setTargetType(1);
										taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
										EventPatrolLineInfo line = patrolLineInfoMapper.selectById(lineInfo1.getId());
										if (line != null) {
											taskInfo.setTargetId(line.getPointId());
										}
										ret = eventPlanTaskInfoMapper.insert(taskInfo);

										if (ret <= 0) {
											throw new TransactionalException("eventPlanTaskInfoMapper insert error");
										}
									}
								}
							}
						}
					}
					//保洁
					else if (eventPlan.getEventTypeId() == 3|| eventPlan.getEventTypeId() == 4 || eventPlan.getEventTypeId() == 5) {
						if (info.getFinishType() == 0) {
							QueryWrapper<EventCleanAreaListInfo> listInfo = new QueryWrapper<>();
							listInfo.eq("area_list_id", info.getGroupId());
//							listInfo.ne("state",0);
							List<EventCleanAreaListInfo> lineInfos = cleanAreaListInfoMapper.selectList(listInfo);
							if (CollUtil.isNotEmpty(lineInfos)) {
								for (EventCleanAreaListInfo lineInfo1 : lineInfos) {
									EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
									taskInfo.setEventWorkId(work.getId());
									taskInfo.setState(0L);
									taskInfo.setTargetType(2);
									taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
									EventCleanAreaListInfo line = cleanAreaListInfoMapper.selectById(lineInfo1.getId());
									if (line != null) {
										taskInfo.setTargetId(Math.toIntExact(line.getAreaId()));
									}
									ret = eventPlanTaskInfoMapper.insert(taskInfo);

									if (ret <= 0) {
										throw new TransactionalException("eventPlanTaskInfoMapper insert error");
									}
								}
							}
						} else {
							for (Integer user : info.getUserId()) {
								SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(user));
								QueryWrapper<EventCleanAreaListInfo> lineInfo = new QueryWrapper<>();
								lineInfo.eq("line_id", info.getGroupId());
//								lineInfo.ne("state",0);
								List<EventCleanAreaListInfo> lineInfos = cleanAreaListInfoMapper.selectList(lineInfo);
								if (CollUtil.isNotEmpty(lineInfos)) {
									for (EventCleanAreaListInfo lineInfo1 : lineInfos) {
										EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
										taskInfo.setEventWorkId(work.getId());
										if (sysUser != null) {
											taskInfo.setSysUserId(Long.valueOf(user));
											if (sysUser.getNickName() != null) {
												taskInfo.setSysUserName(sysUser.getNickName());
											}
											if (sysUser.getWxUserId() != null) {
												taskInfo.setWxUserId(Long.valueOf(sysUser.getWxUserId()));
											}
										}
										taskInfo.setState(0L);
										taskInfo.setTargetType(2);
										taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
										EventCleanAreaListInfo line = cleanAreaListInfoMapper.selectById(lineInfo1.getId());
										if (line != null) {
											taskInfo.setTargetId(Math.toIntExact(line.getAreaId()));
										}
										ret = eventPlanTaskInfoMapper.insert(taskInfo);

										if (ret <= 0) {
											throw new TransactionalException("eventPlanTaskInfoMapper insert error");
										}
									}
								}
							}
						}
					}
					//保洁、绿化、消杀
//					else if (eventPlan.getEventTypeId() == 3 || eventPlan.getEventTypeId() == 4 || eventPlan.getEventTypeId() == 5) {
//						if (info.getFinishType() == 0) {
//							QueryWrapper<EventCleanAreaInfo> areaInfo = new QueryWrapper<>();
//							areaInfo.eq("area_id", info.getGroupId());
//							List<EventCleanAreaInfo> areaInfos = eventCleanAreaInfoMapper.selectList(areaInfo);
//							if (CollUtil.isNotEmpty(areaInfos)) {
//								for (EventCleanAreaInfo areaInfo1 : areaInfos) {
//									EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
//									taskInfo.setEventWorkId(work.getId());
//									taskInfo.setState(0L);
////								if (eventPlan.getEventTypeId()==3){
////									taskInfo.setTargetType(2);
////								}
////								if (eventPlan.getEventTypeId()==4){
////									taskInfo.setTargetType(3);
////								}
////								if (eventPlan.getEventTypeId()==5){
////									taskInfo.setTargetType(4);
////								}
//									taskInfo.setTargetType(2);
//									taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
//									taskInfo.setTargetId(areaInfo1.getId());
//									ret = eventPlanTaskInfoMapper.insert(taskInfo);
//
//									if (ret <= 0) {
//										throw new TransactionalException("eventPlanTaskInfoMapper insert error");
//									}
//								}
//							}
//						} else {
//							for (Integer user : info.getUserId()) {
//								SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(user));
//								QueryWrapper<EventCleanAreaInfo> areaInfo = new QueryWrapper<>();
//								areaInfo.eq("area_id", info.getGroupId());
//								List<EventCleanAreaInfo> areaInfos = eventCleanAreaInfoMapper.selectList(areaInfo);
//								if (CollUtil.isNotEmpty(areaInfos)) {
//									for (EventCleanAreaInfo areaInfo1 : areaInfos) {
//										EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
//										taskInfo.setEventWorkId(work.getId());
//										taskInfo.setState(0L);
//										taskInfo.setTargetType(2);
//										if (sysUser != null) {
//											taskInfo.setSysUserId(Long.valueOf(user));
//											if (sysUser.getNickName() != null) {
//												taskInfo.setSysUserName(sysUser.getNickName());
//											}
//											if (sysUser.getWxUserId() != null) {
//												taskInfo.setWxUserId(Long.valueOf(sysUser.getWxUserId()));
//											}
//										}
//										taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
//										taskInfo.setTargetId(areaInfo1.getId());
//										ret = eventPlanTaskInfoMapper.insert(taskInfo);
//
//										if (ret <= 0) {
//											throw new TransactionalException("eventPlanTaskInfoMapper insert error");
//										}
//									}
//								}
//							}
//						}
//					}
					//生成 task
					for (SysUser user : sysUsers) {
						EventPlanTask task = new EventPlanTask();
						task.setPropertyId(eventPlan.getPropertyId());
						task.setEstateId(eventPlan.getEstateId());
						task.setEventPlanId(eventPlan.getId());
						task.setEventType(eventPlan.getEventTypeId());
						task.setEventTypeName(eventPlan.getEventTypeName());
						task.setEventTargetId(info.getGroupId());
						task.setWorkId(work.getId());
						if (user != null) {
							if (user.getUserId() != null) {
								task.setWorkerSysUserId(Math.toIntExact(user.getUserId()));
							}
							if (user.getNickName() != null) {
								task.setWorkerName(user.getNickName());
							}
							if (user.getWxUserId() != null) {
								task.setWorkerWxUserId(user.getWxUserId());
							}
							if (user.getPhoneNum() != null) {
								task.setWorkerPhone(user.getPhoneNum());
							}
						}
						task.setState(0);
//						task.setId(null);
						task.setTaskBeginTime(info.getBeginRangeTime());
						task.setTaskEndTime(info.getEndRangeTime());
						SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						String time1 = simpleDateFormat1.format(date);
						String beginTime2 = time1.replace("00:00:00", info.getBeginRangeTime() + ":00");
						String endTime2 = time1.replace("00:00:00", info.getEndRangeTime() + ":00");
						try {
							Date beginDate = simpleDateFormat1.parse(beginTime2);
							Date endDate = simpleDateFormat1.parse(endTime2);
							task.setBeginTime(beginDate);
							task.setEndTime(endDate);
						} catch (ParseException e) {
							throw new RuntimeException(e);
						}
						eventPlanTaskMapper.insert(task);
						if (ret <= 0) {
							throw new TransactionalException("eventPlanTaskMapper insert error");
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new TransactionalException("addPlan System error");
		}
		return ret;
	}

	@Override
	@Transactional
	public int editPlan(EventPlan eventPlan) {
		int ret = 0;
		try {

			//删除负责人
			QueryWrapper<EventPlanTaskLeader> qwLeader = new QueryWrapper<>();
			qwLeader.eq("event_plan_id", eventPlan.getId());
			List<EventPlanTaskLeader> leaderList = planTaskLeaderMapper.selectList(qwLeader);
			for (EventPlanTaskLeader leader : leaderList) {
				ret = planTaskLeaderMapper.deleteById(leader.getId());
				if (ret <= 0) {
					throw new TransactionalException("planTaskLeaderMapper deleteById error");
				}
			}
			//删除所有相关任务
			QueryWrapper<EventPlanTask> qw = new QueryWrapper<>();
			qw.eq("event_plan_id", eventPlan.getId());
			List<EventPlanTask> taskList = eventPlanTaskMapper.selectList(qw);
			if (taskList.size() > 0) {
				for (EventPlanTask task : taskList) {
					if (task.getState() == 0) {
						ret = eventPlanTaskMapper.deleteById(task.getId());
					}
					if (ret <= 0) {
						throw new TransactionalException("planTaskLeaderMapper deleteById error");
					}
				}
			}
			//删除所有相关任务状态
			QueryWrapper<EventPlanWork> qw1 = new QueryWrapper<>();
			qw1.eq("event_plan_id", eventPlan.getId());
			List<EventPlanWork> workList = eventPlanWorkMapper.selectList(qw1);
			if (workList.size() > 0) {
				for (EventPlanWork work : workList) {
					if (work.getState() == 0) {
						ret = eventPlanWorkMapper.deleteById(work.getId());
					}
					if (ret <= 0) {
						throw new TransactionalException("eventPlanWorkMapper deleteById error");
					}
				}
			}
			//删除相关任务详情
			QueryWrapper<EventPlanTaskInfo> qw2 = new QueryWrapper<>();
			qw2.eq("event_plan_id", eventPlan.getId());
			List<EventPlanTaskInfo> taskInfos = eventPlanTaskInfoMapper.selectList(qw2);
			if (taskInfos.size() > 0) {
				for (EventPlanTaskInfo taskInfo : taskInfos) {
					if (taskInfo.getState() == 0) {
						ret = eventPlanTaskInfoMapper.deleteById(taskInfo.getId());
					}
					if (ret <= 0) {
						throw new TransactionalException("eventPlanWorkMapper deleteById error");
					}
				}
			}
			//获取区间内的天数
			ArrayList<Date> day = new ArrayList<>();
			Instant begin = eventPlan.getBeginTime().toInstant();
			Instant end = eventPlan.getEndTime().toInstant();
			ZoneId zoneId = ZoneId.systemDefault();
			LocalDate beginTime = begin.atZone(zoneId).toLocalDate();
			LocalDate endTime = end.atZone(zoneId).toLocalDate();
			//两个日期中间有几天
			long days = ChronoUnit.DAYS.between(beginTime, endTime);
			//计划周期
			//当计划频率为日
			if (eventPlan.getCycleType().equals("day")) {
				day = selectDayType(beginTime, endTime);
			}
			//当计划周期不为日
			if (eventPlan.getCycleDay() != null) {
				String str = "";
				for (int i = 0; i < eventPlan.getCycleDay().size(); i++) {
					str += eventPlan.getCycleDay().get(i);
					if (i != eventPlan.getCycleDay().size() - 1) {
						str += ",";
					}

					//当计划频率为周
					if (eventPlan.getCycleType().equals("week")) {
						ArrayList<Date> weekDays = selectWeekType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : weekDays) {
							day.add(date);
						}
					}
					//当计划频率为月
					if (eventPlan.getCycleType().equals("month")) {
						ArrayList<Date> monthDays = selectMonthType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
					//当计划频率为季
					if (eventPlan.getCycleType().equals("season")) {
						ArrayList<Date> monthDays = selectSeasonType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
					//当计划频率为半年
					if (eventPlan.getCycleType().equals("half-year")) {
						ArrayList<Date> monthDays = selectHalfYearType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
					//当计划频率为年
					if (eventPlan.getCycleType().equals("year")) {
						ArrayList<Date> monthDays = selectYearType(eventPlan.getCycleDay().get(i), beginTime, endTime);
						for (Date date : monthDays) {
							day.add(date);
						}
					}
				}
				eventPlan.setCycleDays(str);
			}
			ret = eventPlanMapper.updateById(eventPlan);
			if (ret <= 0) {
				throw new TransactionalException("eventPlanMapper updateById error");
			}
			if (eventPlan.getLeaderId() != null) {
				EventPlanTaskLeader leader = new EventPlanTaskLeader();
				leader.setEventPlanId(eventPlan.getId());
				leader.setLeaderSysUserId(eventPlan.getLeaderId());
				SysUser users = sysUserMapper.selectUserById(Long.valueOf(eventPlan.getLeaderId()));
				if (users != null) {
					if (users.getWxUserId() != null) {
						leader.setLeaderWxUserId(users.getWxUserId());
					}
				}
				ret = planTaskLeaderMapper.insert(leader);
				if (ret <= 0) {
					throw new TransactionalException("planTaskLeaderMapper insert error");
				}
			}
			for (EventPlanInfo info : eventPlan.getEventPlanInfos()) {
				List<SysUser> sysUsers = new ArrayList<>();
				for (int id : info.getUserId()) {
					SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(id));
					sysUsers.add(sysUser);
				}
				//生成计划
				//生成work
				for (Date date : day) {
					EventPlanWork work = new EventPlanWork();
					work.setEventPlanId(eventPlan.getId());
					work.setState(0L);
					work.setPropertyId(eventPlan.getPropertyId());
					work.setFinishType(info.getFinishType());
					work.setEstateId(eventPlan.getEstateId());
					work.setEventTargetId(info.getGroupId());
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String time = simpleDateFormat.format(date);
					String beginTime1 = time.replace("00:00:00", info.getBeginRangeTime() + ":00");
					String endTime1 = time.replace("00:00:00", info.getEndRangeTime() + ":00");
					try {
						Date beginDate = simpleDateFormat.parse(beginTime1);
						Date endDate = simpleDateFormat.parse(endTime1);
						work.setBeginTime(beginDate);
						work.setEndTime(endDate);
					} catch (ParseException e) {
						throw new RuntimeException(e);
					}
					ret = eventPlanWorkMapper.insert(work);
					if (ret <= 0) {
						throw new TransactionalException("eventPlanWorkMapper insert error");
					}

					//生成 info
					//巡检
					if (eventPlan.getEventTypeId() == 1 || eventPlan.getEventTypeId() == 6) {
						if (info.getFinishType() == 0) {

							QueryWrapper<EventTerminals> terminals = new QueryWrapper<>();
							terminals.eq("group_id", info.getGroupId());
							List<EventTerminals> terminals1 = eventTerminalsMapper.selectList(terminals);
							if (CollUtil.isNotEmpty(terminals1)) {
								for (EventTerminals terminals2 : terminals1) {
									EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
									taskInfo.setId(null);
									taskInfo.setEventWorkId(work.getId());
									taskInfo.setState(0L);
									taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
									taskInfo.setTargetId(terminals2.getId());
									if (eventPlan.getEventTypeId() == 1) {
										taskInfo.setTargetType(0);
									} else {
										taskInfo.setTargetType(5);
									}
									ret = eventPlanTaskInfoMapper.insert(taskInfo);
									if (ret <= 0) {
										throw new TransactionalException("eventPlanTaskInfoMapper insert error");
									}
								}
							}
						} else {
							for (Integer user : info.getUserId()) {
								SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(user));
								QueryWrapper<EventTerminals> terminals = new QueryWrapper<>();
								terminals.eq("group_id", info.getGroupId());
								List<EventTerminals> terminals1 = eventTerminalsMapper.selectList(terminals);
								if (CollUtil.isNotEmpty(terminals1)) {
									for (EventTerminals terminals2 : terminals1) {
										EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
										taskInfo.setEventWorkId(work.getId());
										taskInfo.setState(0L);
										taskInfo.setId(null);
										taskInfo.setTargetType(0);
										if (sysUser != null) {
											taskInfo.setSysUserId(Long.valueOf(user));
											if (sysUser.getNickName() != null) {
												taskInfo.setSysUserName(sysUser.getNickName());
											}
											if (sysUser.getWxUserId() != null) {
												taskInfo.setWxUserId(Long.valueOf(sysUser.getWxUserId()));
											}
										}
										taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
										taskInfo.setTargetId(terminals2.getId());
										ret = eventPlanTaskInfoMapper.insert(taskInfo);
										if (ret <= 0) {
											throw new TransactionalException("eventPlanTaskInfoMapper insert error");
										}
									}
								}
							}
						}
					}
					//巡更
					if (eventPlan.getEventTypeId() == 2) {
						if (info.getFinishType() == 0) {
							QueryWrapper<EventPatrolLineInfo> lineInfo = new QueryWrapper<>();
							lineInfo.eq("line_id", info.getGroupId());
							List<EventPatrolLineInfo> lineInfos = patrolLineInfoMapper.selectList(lineInfo);
							if (CollUtil.isNotEmpty(lineInfos)) {
								for (EventPatrolLineInfo lineInfo1 : lineInfos) {
									EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
									taskInfo.setEventWorkId(work.getId());
									taskInfo.setState(0L);
									taskInfo.setTargetType(1);
									taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
									EventPatrolLineInfo line = patrolLineInfoMapper.selectById(lineInfo1.getId());
									if (line != null) {
										taskInfo.setTargetId(line.getPointId());
									}
									ret = eventPlanTaskInfoMapper.insert(taskInfo);
									if (ret <= 0) {
										throw new TransactionalException("eventPlanTaskInfoMapper insert error");
									}
								}
							}
						} else {
							for (Integer user : info.getUserId()) {
								SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(user));
								QueryWrapper<EventPatrolLineInfo> lineInfo = new QueryWrapper<>();
								lineInfo.eq("line_id", info.getGroupId());
								List<EventPatrolLineInfo> lineInfos = patrolLineInfoMapper.selectList(lineInfo);
								if (CollUtil.isNotEmpty(lineInfos)) {
									for (EventPatrolLineInfo lineInfo1 : lineInfos) {
										EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
										taskInfo.setEventWorkId(work.getId());
										if (sysUser != null) {
											taskInfo.setSysUserId(Long.valueOf(user));
											if (sysUser.getNickName() != null) {
												taskInfo.setSysUserName(sysUser.getNickName());
											}
											if (sysUser.getWxUserId() != null) {
												taskInfo.setWxUserId(Long.valueOf(sysUser.getWxUserId()));
											}
										}
										taskInfo.setState(0L);
										taskInfo.setTargetType(1);
										taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
										EventPatrolLineInfo line = patrolLineInfoMapper.selectById(lineInfo1.getId());
										if (line != null) {
											taskInfo.setTargetId(line.getPointId());
										}
										ret = eventPlanTaskInfoMapper.insert(taskInfo);
										if (ret <= 0) {
											throw new TransactionalException("eventPlanTaskInfoMapper insert error");
										}
									}
								}
							}
						}
					}
					//保洁
					else if (eventPlan.getEventTypeId() == 3|| eventPlan.getEventTypeId() == 4 || eventPlan.getEventTypeId() == 5) {
						if (info.getFinishType() == 0) {
							QueryWrapper<EventCleanAreaListInfo> listInfo = new QueryWrapper<>();
							listInfo.eq("area_list_id", info.getGroupId());
							List<EventCleanAreaListInfo> lineInfos = cleanAreaListInfoMapper.selectList(listInfo);
							if (CollUtil.isNotEmpty(lineInfos)) {
								for (EventCleanAreaListInfo lineInfo1 : lineInfos) {
									EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
									taskInfo.setEventWorkId(work.getId());
									taskInfo.setState(0L);
									taskInfo.setTargetType(2);
									taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
									EventCleanAreaListInfo line = cleanAreaListInfoMapper.selectById(lineInfo1.getId());
									if (line != null) {
										taskInfo.setTargetId(Math.toIntExact(line.getAreaId()));
									}
									ret = eventPlanTaskInfoMapper.insert(taskInfo);

									if (ret <= 0) {
										throw new TransactionalException("eventPlanTaskInfoMapper insert error");
									}
								}
							}
						} else {
							for (Integer user : info.getUserId()) {
								SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(user));
								QueryWrapper<EventCleanAreaListInfo> lineInfo = new QueryWrapper<>();
								lineInfo.eq("line_id", info.getGroupId());
								List<EventCleanAreaListInfo> lineInfos = cleanAreaListInfoMapper.selectList(lineInfo);
								if (CollUtil.isNotEmpty(lineInfos)) {
									for (EventCleanAreaListInfo lineInfo1 : lineInfos) {
										EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
										taskInfo.setEventWorkId(work.getId());
										if (sysUser != null) {
											taskInfo.setSysUserId(Long.valueOf(user));
											if (sysUser.getNickName() != null) {
												taskInfo.setSysUserName(sysUser.getNickName());
											}
											if (sysUser.getWxUserId() != null) {
												taskInfo.setWxUserId(Long.valueOf(sysUser.getWxUserId()));
											}
										}
										taskInfo.setState(0L);
										taskInfo.setTargetType(2);
										taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
										EventCleanAreaListInfo line = cleanAreaListInfoMapper.selectById(lineInfo1.getId());
										if (line != null) {
											taskInfo.setTargetId(Math.toIntExact(line.getAreaId()));
										}
										ret = eventPlanTaskInfoMapper.insert(taskInfo);

										if (ret <= 0) {
											throw new TransactionalException("eventPlanTaskInfoMapper insert error");
										}
									}
								}
							}
						}
					}
					//保洁、绿化、消杀
//					if (eventPlan.getEventTypeId() == 3 || eventPlan.getEventTypeId() == 4 || eventPlan.getEventTypeId() == 5) {
//						if (info.getFinishType() == 0) {
//							EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
//							taskInfo.setEventWorkId(work.getId());
//							taskInfo.setState(0L);
////								if (eventPlan.getEventTypeId()==3){
////									taskInfo.setTargetType(2);
////								}
////								if (eventPlan.getEventTypeId()==4){
////									taskInfo.setTargetType(3);
////								}
////								if (eventPlan.getEventTypeId()==5){
////									taskInfo.setTargetType(4);
////								}
//							taskInfo.setTargetType(2);
//							taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
//							taskInfo.setTargetId(Integer.valueOf(info.getGroupId()));
//
//							ret = eventPlanTaskInfoMapper.insert(taskInfo);
//							if (ret <= 0) {
//								throw new TransactionalException("eventPlanTaskInfoMapper insert error");
//							}
//
//
//						} else {
//							for (Integer user : info.getUserId()) {
//								SysUser sysUser = sysUserMapper.selectUserById(Long.valueOf(user));
//								EventPlanTaskInfo taskInfo = new EventPlanTaskInfo();
//								taskInfo.setEventWorkId(work.getId());
//								taskInfo.setState(0L);
//								taskInfo.setTargetType(2);
//								if (sysUser != null) {
//									taskInfo.setSysUserId(Long.valueOf(user));
//									if (sysUser.getNickName() != null) {
//										taskInfo.setSysUserName(sysUser.getNickName());
//									}
//									if (sysUser.getWxUserId() != null) {
//										taskInfo.setWxUserId(Long.valueOf(sysUser.getWxUserId()));
//									}
//								}
//								taskInfo.setEventPlanId(Long.valueOf(eventPlan.getId()));
//								taskInfo.setTargetId(info.getGroupId());
//								ret = eventPlanTaskInfoMapper.insert(taskInfo);
//								if (ret <= 0) {
//									throw new TransactionalException("eventPlanTaskInfoMapper insert error");
//								}
//							}
//						}
//					}

					//生成task
					for (SysUser user : sysUsers) {
						EventPlanTask task = new EventPlanTask();
						task.setPropertyId(eventPlan.getPropertyId());
						task.setEstateId(eventPlan.getEstateId());
						task.setEventPlanId(eventPlan.getId());
						task.setEventType(eventPlan.getEventTypeId());
						task.setEventTypeName(eventPlan.getEventTypeName());
						task.setEventTargetId(info.getGroupId());
						task.setWorkId(work.getId());
						if (user != null) {
							if (user.getUserId() != null) {
								task.setWorkerSysUserId(Math.toIntExact(user.getUserId()));
							}
							if (user.getNickName() != null) {
								task.setWorkerName(user.getNickName());
							}
							if (user.getWxUserId() != null) {
								task.setWorkerWxUserId(user.getWxUserId());
							}
							if (user.getPhoneNum() != null) {
								task.setWorkerPhone(user.getPhoneNum());
							}
						}
						task.setState(0);
//						task.setId(null);
						task.setTaskBeginTime(info.getBeginRangeTime());
						task.setTaskEndTime(info.getEndRangeTime());
						SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
						String time1 = simpleDateFormat1.format(date);
						String beginTime2 = time1.replace("00:00:00", info.getBeginRangeTime() + ":00");
						String endTime2 = time1.replace("00:00:00", info.getEndRangeTime() + ":00");
						try {
							Date beginDate = simpleDateFormat1.parse(beginTime2);
							Date endDate = simpleDateFormat1.parse(endTime2);
							task.setBeginTime(beginDate);
							task.setEndTime(endDate);
						} catch (ParseException e) {
							throw new RuntimeException(e);
						}
						ret = eventPlanTaskMapper.insert(task);
						if (ret <= 0) {
							throw new TransactionalException("eventPlanTaskMapper insert error");
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new TransactionalException("eventPlanTaskMapper edit error");
		}
		return ret;
	}

	//当计划频率为 日
	@Override
	public ArrayList<Date> selectDayType(LocalDate beginTime, LocalDate endTime) {
		ArrayList<Date> day = new ArrayList<>();
		long days = ChronoUnit.DAYS.between(beginTime, endTime);
		Calendar startCalender = GregorianCalendar.from(beginTime.atStartOfDay(ZoneId.systemDefault()));
		beginTime = LocalDateTime.ofInstant(startCalender.toInstant(), ZoneOffset.systemDefault()).toLocalDate();
		ZoneId zone1 = ZoneId.systemDefault();
		Instant instant1 = beginTime.atStartOfDay().atZone(zone1).toInstant();
		Date date1 = Date.from(instant1);
		day.add(date1);
		for (int j = 0; j < days; j++) {
			startCalender.add(Calendar.DATE, 1);
			LocalDate localDate = LocalDateTime.ofInstant(startCalender.toInstant(), ZoneOffset.systemDefault()).toLocalDate();
			ZoneId zone = ZoneId.systemDefault();
			Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
			Date date = Date.from(instant);
			day.add(date);
		}
		return day;
	}

	//当计划频率为 周
	@Override
	public ArrayList<Date> selectWeekType(String weekDay, LocalDate beginTime, LocalDate endTime) {
		beginTime = beginTime.plusDays(-1);
		ArrayList<Date> day = new ArrayList<>();
		long days = ChronoUnit.DAYS.between(beginTime, endTime);
		Calendar startCalender = GregorianCalendar.from(beginTime.atStartOfDay(ZoneId.systemDefault()));
		for (int j = 0; j < days; j++) {
			startCalender.add(Calendar.DATE, 1);
			int week = Integer.parseInt(weekDay);
			if (startCalender.get(Calendar.DAY_OF_WEEK) == week) {
				LocalDate localDate = LocalDateTime.ofInstant(startCalender.toInstant(), ZoneOffset.systemDefault()).toLocalDate();
				ZoneId zone = ZoneId.systemDefault();
				Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
				Date date = Date.from(instant);
				day.add(date);
			}
		}
		return day;
	}


	//当计划频率为 月
	@Override
	public ArrayList<Date> selectMonthType(String monthDay, LocalDate beginTime, LocalDate endTime) {
		beginTime = beginTime.plusDays(-1);
		ArrayList<Date> day = new ArrayList<>();
		long days = ChronoUnit.DAYS.between(beginTime, endTime);
		Calendar startCalender = GregorianCalendar.from(beginTime.atStartOfDay(ZoneId.systemDefault()));
		for (int j = 0; j < days; j++) {
			startCalender.add(Calendar.DATE, 1);
			int month = Integer.parseInt(monthDay);
			if (startCalender.get(Calendar.DAY_OF_MONTH) == month) {
				LocalDate localDate = LocalDateTime.ofInstant(startCalender.toInstant(), ZoneOffset.systemDefault()).toLocalDate();
				ZoneId zone = ZoneId.systemDefault();
				Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
				Date date = Date.from(instant);
				day.add(date);
			}
		}
		return day;
	}

	//当计划频率为 季
	@Override
	public ArrayList<Date> selectSeasonType(String seasonDay, LocalDate beginTime, LocalDate endTime) {
		beginTime = beginTime.plusDays(-1);
		ArrayList<Date> day = new ArrayList<>();
		long days = ChronoUnit.DAYS.between(beginTime, endTime);
		Calendar startCalender = GregorianCalendar.from(beginTime.atStartOfDay(ZoneId.systemDefault()));
		for (int j = 0; j < days; j++) {
			startCalender.add(Calendar.DATE, 1);
			int month = Integer.parseInt(seasonDay);
			if (startCalender.get(Calendar.DAY_OF_MONTH) == month) {
				LocalDate localDate = LocalDateTime.ofInstant(startCalender.toInstant(), ZoneOffset.systemDefault()).toLocalDate();
				ZoneId zone = ZoneId.systemDefault();
				Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
				Date date = Date.from(instant);
				Calendar calender = Calendar.getInstance();
				calender.setTime(date);
				if (calender.get(Calendar.MONTH) + 1 == 1 || calender.get(Calendar.MONTH) + 1 == 4 || calender.get(Calendar.MONTH) + 1 == 7 || calender.get(Calendar.MONTH) + 1 == 10) {
					day.add(date);
				}
			}
		}
		return day;
	}

	//当计划频率为 半年
	@Override
	public ArrayList<Date> selectHalfYearType(String halfYearDay, LocalDate beginTime, LocalDate endTime) {
		beginTime = beginTime.plusDays(-1);
		ArrayList<Date> day = new ArrayList<>();
		long days = ChronoUnit.DAYS.between(beginTime, endTime);
		Calendar startCalender = GregorianCalendar.from(beginTime.atStartOfDay(ZoneId.systemDefault()));
		for (int j = 0; j < days; j++) {
			startCalender.add(Calendar.DATE, 1);
			int month = Integer.parseInt(halfYearDay);
			if (startCalender.get(Calendar.DAY_OF_MONTH) == month) {
				LocalDate localDate = LocalDateTime.ofInstant(startCalender.toInstant(), ZoneOffset.systemDefault()).toLocalDate();
				ZoneId zone = ZoneId.systemDefault();
				Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
				Date date = Date.from(instant);
				Calendar calender = Calendar.getInstance();
				calender.setTime(date);
				if (calender.get(Calendar.MONTH) + 1 == 1 || calender.get(Calendar.MONTH) + 1 == 7) {
					day.add(date);
				}
			}
		}
		return day;
	}

	//当计划频率为 年
	@Override
	public ArrayList<Date> selectYearType(String yearDay, LocalDate beginTime, LocalDate endTime) {
		beginTime = beginTime.plusDays(-1);
		ArrayList<Date> day = new ArrayList<>();
		long days = ChronoUnit.DAYS.between(beginTime, endTime);
		Calendar startCalender = GregorianCalendar.from(beginTime.atStartOfDay(ZoneId.systemDefault()));
		for (int j = 0; j < days; j++) {
			startCalender.add(Calendar.DATE, 1);
			int month = Integer.parseInt(yearDay);
			if (startCalender.get(Calendar.DAY_OF_MONTH) == month) {
				LocalDate localDate = LocalDateTime.ofInstant(startCalender.toInstant(), ZoneOffset.systemDefault()).toLocalDate();
				ZoneId zone = ZoneId.systemDefault();
				Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
				Date date = Date.from(instant);
				Calendar calender = Calendar.getInstance();
				calender.setTime(date);
				if (calender.get(Calendar.MONTH) + 1 == 1) {
					day.add(date);
				}
			}
		}
		return day;
	}
}
